<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
//https请求（支持GET和POST）
function https_request($url, $data = NULL, $username = NULL, $password = NULL) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
//    curl_setopt($curl, CURLOPT_SSLVERSION, 3);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 20); //timeout on connect
    curl_setopt($curl, CURLOPT_TIMEOUT, 20); //timeout on response
    if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    if (!empty($username) && !empty($password)) {
        $headers = array(
            'Authorization: Basic ' . base64_encode("{$username}:{$password}")
        );
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    }
    if (!empty($username) && empty($password)) {
        curl_setopt($curl, CURLOPT_HTTPHEADER, $username);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    if (curl_error($curl) && empty($output)) $output = curl_error($curl);
    curl_close($curl);
    return $output;
}

